package com.example.users.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.users.domain.Admin;
import com.example.users.service.AdminService;
import com.example.users.common.PageResponse;
import com.example.users.common.PageUtil;
import com.example.users.common.ApiResponse;
import com.example.users.common.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

@RestController
@RequestMapping("/api/admins")
public class AdminController {

    private final AdminService adminService;

    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @GetMapping
    public ApiResponse<PageResponse<Admin>> list(@RequestParam(defaultValue = "1") long page,
                                  @RequestParam(defaultValue = "10") long size,
                                  @RequestParam(required = false) String keyword,
                                  @RequestParam(required = false, defaultValue = "id") String sortBy,
                                  @RequestParam(required = false, defaultValue = "desc") String sortOrder) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            qw.lambda()
              .like(Admin::getUsername, keyword)
              .or().like(Admin::getNickname, keyword);
        }
        // sort whitelist
        if ("username".equalsIgnoreCase(sortBy)) {
            qw.orderBy(true, "asc".equalsIgnoreCase(sortOrder), "username");
        } else if ("nickname".equalsIgnoreCase(sortBy)) {
            qw.orderBy(true, "asc".equalsIgnoreCase(sortOrder), "nickname");
        } else {
            qw.orderBy(true, "asc".equalsIgnoreCase(sortOrder), "id");
        }
        Page<Admin> result = adminService.page(Page.of(page, size), qw);
        return ApiResponse.success(PageUtil.toResponse(result));
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> detail(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return ResponseEntity.ok(admin);
    }

    @PostMapping
    public ApiResponse<?> create(@Validated(com.example.users.validation.groups.Create.class) @RequestBody Admin admin) {
        admin.setId(null);
        boolean ok = adminService.save(admin);
        return ApiResponse.success(ok ? admin : null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @Validated(com.example.users.validation.groups.Update.class) @RequestBody Admin admin) {
        admin.setId(id);
        boolean ok = adminService.updateById(admin);
        return ApiResponse.success(ok ? admin : null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> delete(@PathVariable Long id) {
        boolean ok = adminService.removeById(id);
        return ApiResponse.success(ok);
    }
}


